var appID = '135216369868379';
var fbReady = false;
var jQueryReady = false;

window.onload = loadScript;
window.fbAsyncInit = initializeViaFacebook;

function createProfilePic(i, item) {
	// profile pic button
	var profilePic = document.createElement("fb:profile-pic");
	profilePic.setAttribute('uid', item.facebookID);
	profilePic.setAttribute('linked', "true");
	profilePic.setAttribute('size', "normal");
	
	switch(i) {
	case 0:
		profilePic.setAttribute('width', "200");
		$("#firstPlaceDiv").append(profilePic);
		break;
	case 1:
		profilePic.setAttribute('width', "150");
		$("#secondPlaceDiv").append(profilePic);
		break;
	case 2:
		profilePic.setAttribute('width', "100");
		$("#thirdPlaceDiv").append(profilePic);
		break;
	case 3:
		profilePic.setAttribute('width', "75");
		$("#fourthPlaceDiv").append(profilePic);
		break;
	case 4:
		profilePic.setAttribute('width', "75");
		$("#fifthPlaceDiv").append(profilePic);
		break;
	case 5:
		profilePic.setAttribute('width', "75");
		$("#sixthPlaceDiv").append(profilePic);
		break;
	case 6:
		profilePic.setAttribute('width', "50");
		$("#seventhPlaceDiv").append(profilePic);
		break;
	case 7:
		profilePic.setAttribute('width', "50");
		$("#eighthPlaceDiv").append(profilePic);
		break;
	case 8:
		profilePic.setAttribute('width', "50");
		$("#ninethPlaceDiv").append(profilePic);
		break;
	case 9:
		profilePic.setAttribute('width', "50");
		$("#tenthPlaceDiv").append(profilePic);
		break;
	}
}

// jQuery and Facebook scripts need to be ready before DOM manipulation can begin
// checks to see if jQuery has already been loaded and if so, begins the manipulation
function initializeViaFacebook() {
	FB.init({
		appId  : appID,
		status : true, // check login status
		cookie : true, // enable cookies to allow the server to access the session
		xfbml  : false  // parse XFBML
	  });
	fbReady = true;
	if(jQueryReady)
		loadUsers();
}

// jQuery and Facebook scripts need to be ready before DOM manipulation can begin
// checks to see if Facebook has already been loaded and if so, begins the manipulation
function initializeViaJQuery() {
	jQueryReady = true;
	if(fbReady)
		loadUsers();
}

// load fb js file async
function loadScript() { 
  var facebookScript = document.createElement('script'); 
  facebookScript.async = true;
  facebookScript.src = document.location.protocol + '//connect.facebook.net/en_US/all.js';
  document.body.appendChild(facebookScript);
}

// load top 10 facebook users
function loadUsers() {
	$.getJSON("/resources/facebook/topten",
		function(data) {
			$.each(data.users, createProfilePic);
			FB.XFBML.parse();
		}
	);
}